package com.nit.lab.mapper.basic;

import com.nit.lab.dto.basic.UserDTO;
import com.nit.lab.entity.basic.UserEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {

    /**
     * @Description: 获取表格数据，查询条件自行编写对应代码，需区分模糊和相等，排序条件无需改动
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select("<script>SELECT " +
            "user_id AS `key`, user_id, " +
            "login_name, role_id, user_name, " +
            "user_image, user_info, basic_user.gmt_create " +
            "FROM basic_user " +
            "WHERE true " +
            "<if test='loginName != null'> AND login_name LIKE CONCAT('%', #{loginName}, '%') </if>" +
            "<if test='userName != null'> AND user_name LIKE CONCAT('%', #{userName}, '%') </if>" +
            "ORDER BY " +
            "<if test='sortField != null'>" +
            "${sortField} ${sortOrder}, " +
            "</if>" +
            "user_id desc " +
            "LIMIT #{start}, #{length}" +
            "</script>")
    List<UserDTO> list4Table(
            @Param("start") Integer start,
            @Param("length") Integer length,
            @Param("loginName") String loginName,
            @Param("userName") String userName,
            @Param("sortField") String sortField,
            @Param("sortOrder") String sortOrder) throws Exception;

    /**
     * @Description: 获取表格数据记录的总条数
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select("<script>SELECT COUNT(*) " +
            "FROM basic_user " +
            "WHERE true " +
            "<if test='loginName != null'> AND login_name LIKE CONCAT('%', #{loginName}, '%') </if> " +
            "<if test='userName != null'> AND user_name LIKE CONCAT('%', #{userName}, '%') </if> " +
            "</script>")
    Long count4Table(
            @Param("loginName") String loginName,
            @Param("userName") String userName) throws Exception;

    /**
     * @Description: 新增记录
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Insert("INSERT INTO basic_user ( " +
            "login_name, login_pwd, " +
            "user_name, user_image, user_info, " +
            "role_id, gmt_create) " +
            "VALUES (#{userEntity.loginName}, #{userEntity.loginPwd}, " +
            "#{userEntity.userName}, #{userEntity.userImage}, #{userEntity.userInfo}, " +
            "#{userEntity.roleId},  " +
            "#{userEntity.gmtCreate})")
    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
    Integer add(@Param("userEntity") UserEntity userEntity) throws Exception;

    /**
     * @Description: 更新记录
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Update("UPDATE basic_user " +
            "SET login_name = #{userEntity.loginName}, " +
            "user_name = #{userEntity.userName}, " +
            "user_info = #{userEntity.userInfo}, " +
            "role_id = #{userEntity.roleId}, " +
            "gmt_create = #{userEntity.gmtCreate} " +
            "WHERE user_id = #{userEntity.userId}")
    Integer update(@Param("userEntity") UserEntity userEntity) throws Exception;


    /**
     * @Description: 删除记录
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Delete("DELETE FROM basic_user WHERE user_id = #{userId}")
    Integer remove(@Param("userId") Long userId) throws Exception;

    /**
     * @Description: 重置用户密码
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Update("UPDATE basic_user SET login_pwd = #{loginPwd} WHERE user_id = #{userId}")
    Integer resetPwd(@Param("userId") Long userId,
                     @Param("loginPwd") String loginPwd) throws Exception;


    /**
     * @Description: 根据用户登录名称获取用户信息
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select("SELECT " +
            "user_id, login_name, role_id, " +
            "login_pwd, user_name, " +
            "user_image, user_info, " +
            "basic_user.gmt_create " +
            "FROM basic_user " +
            "WHERE " +
            "login_name = #{loginName} LIMIT 1")
    UserDTO getByLoginName(@Param("loginName") String loginName) throws Exception;

    /**
     * @Description: 根据用户ID获取用户信息
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select("SELECT " +
            "user_id, login_name, role_id, " +
            "login_pwd, user_name, " +
            "user_image, user_info, " +
            "gmt_create " +
            "FROM basic_user " +
            "WHERE user_id = #{userId}")
    UserDTO getByUserId(@Param("userId") Long userId) throws Exception;
}
